<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
          >批量禁用</el-button
        >
        <el-button
          type="success"
          icon="el-icon-plus"
          class="handle-del mr10"
          @click="addOne"
          >增加</el-button
        >
        <el-select
          v-model="query.queryParam.fkCourseName"
          filterable
          placeholder="请选择课程"
          style="width: 150px"
        >
          <el-option value="" label="所有课程"></el-option>
          <el-option v-for="item in courses" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
        <el-select
          v-model="query.queryParam.type"
          placeholder="请选择题目类型"
          style="width: 140px; margin-left: 10px"
        >
          <el-option value="" label="所有类型"></el-option>
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-input
          v-model="query.queryParam.titleDetail"
          placeholder="题目内容"
          class="handle-input mr10"
          style="width: 200px; margin-left: 10px"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          :selectable="checkSelectable"
        ></el-table-column>
        <el-table-column
          prop="fkCourseName"
          label="所属课程名"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="题目类型"
          align="center"
          :formatter="formatteType"
        ></el-table-column>
        <el-table-column
          prop="titleDetail"
          label="题目详情"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="score" label="题目分值" align="center"></el-table-column>
        <el-table-column prop="remarks" label="备注" align="center"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-document"
              @click="openDialog(scope.$index, scope.row, true)"
              >详情</el-button
            >
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="openDialog(scope.$index, scope.row, false)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog
      title="详情"
      :visible.sync="editVisible"
      :close-on-click-modal="false"
      @close="dialogClose"
      width="856.34px"
    >
      <edit-question
        ref="questionform"
        @closeandreload="closeandreload"
        :form="editParam.form"
        :types="types"
        :isdisabled="editParam.isDisable"
        :courses="courses"
      ></edit-question>
    </el-dialog>
  </div>
</template>

<script>
import Constants from "../../utils/Constant";
import EditQuestion from "../page/EditQuestion.vue";
var _this;
export default {
  components: { EditQuestion },
  name: "basetable",
  data() {
    return {
      query: {
        queryParam: {
          type: "",
          titleDetail: "",
          fkCourseName: "",
        },
        pageIndex: 1,
        pageSize: 10,
      },
      tableData: [],
      types: Constants.QuestionType.list,
      courses: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      total: 0,
      editParam: {},
      rowdata: {},
    };
  },
  created() {
    this.getData();
    this.getCourses();
    _this = this;
  },
  methods: {
    // 关闭弹窗 刷新页面
    closeandreload(){
      this.editVisible = false
      this.dialogClose()
      this.getData()
    },
    // 弹窗关闭前监听事件
    dialogClose() {
      this.editParam.form = {
        answer: "",
        fkCourseName: "",
        remarks: "",
        score: "",
        selectionList: [],
        titleDetail: "",
        type: "",
        imgList: [],
      };
      this.$refs.questionform.$refs.editquestion.clearValidate();
    },
    // 格式化题目类型
    formatteType(value) {
      return Constants.QuestionType.getLabelByValue(value.type);
    },
    // 获取数据
    getData() {
      this.$post("/question/list", this.query).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data.records;
          this.total = res.data.total || 1;
        }
      });
    },
    // 获取课程
    getCourses() {
      this.$post("/course/byteacher", null).then((res) => {
        if (res.code === 0) {
          this.courses = res.data;
        }
      });
    },
    // 是否可选中
    checkSelectable(row) {
      return row.isDel === 0;
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delList.push(row.id);
          this.deleteList(this.delList);
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      this.delList = this.multipleSelection.map((x) => {
        return x.id;
      });
      this.deleteList(this.delList);
      this.multipleSelection = [];
    },
    //删除操作
    deleteList(param) {
      this.$post("/question/del", param).then((res) => {
        if (res.code === 0) {
          this.$message.success("删除成功");
          this.getData();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 新增操作
    addOne() {
      const temp = {
        id: 0,
        answer: "",
        fkCourseName: "",
        remarks: "",
        score: "",
        selectionList: [],
        titleDetail: "",
        type: "",
        imgList: null,
      };
      this.openDialog(null, temp, false);
    },
    // 详情页面
    openDialog(index, row, value) {
      this.rowdata = JSON.parse(JSON.stringify(row));
      this.editParam.form = this.rowdata;
      if (row.remarks === null || row.remarks === undefined) {
        this.editParam.form.remarks = "";
      }
      if (row.imgList === null || row.imgList === undefined) {
        this.editParam.form.imgList = [];
      } else {
        this.editParam.form.imgList = JSON.parse(row.imgList);
      }
      if (row.type === "RADIO_QUESTION" || row.type === "MULTI_SELECT_QUESTION") {
        this.editParam.form.selectionList = JSON.parse(row.selectionList);
      }
      if (row.type === "MULTI_SELECT_QUESTION") {
        this.editParam.form.answer = JSON.parse(row.answer);
      }
      this.editParam.isDisable = value;
      this.editVisible = true;
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
  },
  watch: {
    // form: {
    //   handler: (val, oldval) => {
    //     if (JSON.stringify(_this.form) !== JSON.stringify(_this.rowdata)) {
    //       _this.btnBoolean = false;
    //     } else {
    //       _this.btnBoolean = true;
    //     }
    //   },
    //   deep: true,
    // },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 150px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
/deep/.el-upload {
  width: 150px;
  height: 210px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 210px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 210px;
  display: block;
}
.el-tooltip__popper {
  max-width: 500px;
}
/* 谷歌 */
/deep/input::-webkit-outer-spin-button,
/deep/input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}
/* 火狐 */
/deep/input {
  -moz-appearance: textfield;
}
</style>
